package org.elsfs.cloud.common.mybatis.ext.sql.support.order;




import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 多字段排序
 *

 */
@RequiredArgsConstructor
public class CompositeOrderItem implements OrderItem {

    /**
     * 排序条件项列表
     */
    private final List<OrderItem> orderItems;

    public CompositeOrderItem() {
        this.orderItems = Lists.newArrayList();
    }

    /**
     * 添加排序项
     *
     * @param orderItem 排序项
     */
    public void addOrderItem(OrderItem orderItem) {
        this.orderItems.add(orderItem);
    }

    /**
     * 构建SQL片段
     *
     * @return SQL片段
     */
    @Override
    public String buildSqlFragment() {
        return String.format(
            "ORDER BY %s",
            this.orderItems.stream()
                .map(OrderItem::buildSqlFragment)
                .collect(Collectors.joining(", "))
        );
    }
}
